package com.laizhenghua.share.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laizhenghua.share.device.domain.entity.CabinetType;
import com.laizhenghua.share.device.mapper.CabinetTypeMapper;
import com.laizhenghua.share.device.service.ICabinetTypeService;
import com.laizhenghua.share.system.api.model.LoginUser;
import com.share.common.security.utils.SecurityUtils;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author laizh
 * @Date 2025/6/22 18:08
 **/
@Log
@Service
public class ICabinetTypeServiceImpl extends ServiceImpl<CabinetTypeMapper, CabinetType> implements ICabinetTypeService {
    // private final Logger log = LoggerFactory.getLogger(this.getClass());
    private final CabinetTypeMapper cabinetTypeMapper;

    @Autowired
    public ICabinetTypeServiceImpl(CabinetTypeMapper cabinetTypeMapper) {
        this.cabinetTypeMapper = cabinetTypeMapper;
    }
    @Override
    public List<CabinetType> searchPageList(CabinetType cabinetType) {
        List<CabinetType> ls = cabinetTypeMapper.searchPageList(cabinetType);
        if (CollectionUtil.isEmpty(ls)) {
            return List.of();
        }
        return ls;
    }

    @Override
    public CabinetType customSave(CabinetType cabinetType) {
        if (ObjectUtil.isNull(cabinetType.getCreateTime())) {
            cabinetType.setCreateTime(new Date());
        }
        if (StrUtil.isEmpty(cabinetType.getCreateBy())) {
            cabinetType.setCreateBy("admin");
        }
        if (ObjectUtil.isNotNull(cabinetType.getId())) {
            cabinetType.setUpdateTime(new Date());
            LoginUser user = SecurityUtils.getLoginUser();
            if (ObjectUtil.isNotNull(user)) {
                cabinetType.setUpdateBy(user.getUsername());
            }
        }
        boolean s = this.saveOrUpdate(cabinetType);
        if (s) {
            return this.getById(cabinetType.getId());
        }
        return null;
    }
}
